home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / ka9q_src.arc / SESSION.C < prev    next >
C/C++ Source or Header  |  1988-11-30  |  7KB  |  349 lines

  1. /* Session control */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "config.h"
  5. #include "mbuf.h"
  6. #include "netuser.h"
  7. #include "timer.h"
  8. #include "tcp.h"
  9. #include "ax25.h"
  10. #include "lapb.h"
  11. #include "ftp.h"
  12. #include "telnet.h"
  13. #include "session.h"
  14. #include "cmdparse.h"
  15.  
  16. struct session *sessions;
  17. struct session *current;
  18. char notval[] = "Not a valid control block\n";
  19. char badsess[] = "Invalid session\n";
  20.  
  21. /* Convert a character string containing a decimal session index number 
  22.  * into a pointer. If the arg is NULLCHAR, use the current default session.
  23.  * If the index is out of range or unused, return NULLSESSION.
  24.  */
  25. static struct session *
  26. sessptr(cp)
  27. char *cp;
  28. {
  29.     register struct session *s;
  30.     unsigned int i;
  31.  
  32.     if(cp == NULLCHAR){
  33.         s = current;
  34.     } else {
  35.         if((i = atoi(cp)) >= nsessions)
  36.             return NULLSESSION;
  37.         s = &sessions[i];
  38.     }
  39.     if(s == NULLSESSION || s->type == FREE)
  40.         return NULLSESSION;
  41.  
  42.     return s;
  43. }
  44.  
  45. /* Select and display sessions */
  46. dosession(argc,argv)
  47. int argc;
  48. char *argv[];
  49. {
  50.     struct session *s;
  51.     extern char *tcpstates[];
  52.     char *psocket();
  53.     extern char *ax25states[];
  54.  
  55.     if(argc > 1){
  56.         if((current = sessptr(argv[1])) != NULLSESSION)
  57.             go();
  58.         return 0;
  59.     }
  60.     printf(" #     &CB Type   Rcv-Q  State    Remote socket\n");
  61.     for(s=sessions; s < &sessions[nsessions];s++){
  62.         switch(s->type){
  63.         case TELNET:
  64.             printf("%c%-3d%8lx Telnet  %4d  %-13s%-s:%d",
  65.              (current == s)? '*':' ',
  66.              (int)(s - sessions),
  67.              (long)s->cb.telnet->tcb,
  68.              s->cb.telnet->tcb->rcvcnt,
  69.              tcpstates[s->cb.telnet->tcb->state],
  70.              s->name,s->cb.telnet->tcb->conn.remote.port);
  71.             break;
  72.         case FTP:
  73.             printf("%c%-3d%8lx FTP     %4d  %-13s%-s:%d",
  74.              (current == s)? '*':' ',
  75.              (int)(s - sessions),
  76.              (long)s->cb.ftp->control,
  77.              s->cb.ftp->control->rcvcnt,
  78.              tcpstates[s->cb.ftp->control->state],
  79.              s->name,s->cb.ftp->control->conn.remote.port);
  80.             break;
  81. #ifdef    AX25
  82.         case AX25TNC:
  83.             printf("%c%-3d%8lx AX25    %4d  %-13s%-s",
  84.              (current == s)? '*':' ',
  85.              (int)(s - sessions),
  86.              (long)s->cb.ax25_cb,
  87.             len_mbuf(s->cb.ax25_cb->rxq),
  88.              ax25states[s->cb.ax25_cb->state],
  89.              s->name);
  90.             break;
  91. #endif
  92.         default:
  93.             continue;
  94.         }
  95.         if(s->rfile != NULLCHAR || s->ufile != NULLCHAR)
  96.             printf("\t");
  97.         if(s->rfile != NULLCHAR)
  98.             printf("Record: %s ",s->rfile);
  99.         if(s->ufile != NULLCHAR)
  100.             printf("Upload: %s",s->ufile);
  101.         printf("\n");
  102.     }
  103.     return 0;
  104. }
  105. /* Enter conversational mode with current session */
  106. int
  107. go()
  108. {
  109.     void rcv_char(),ftpccr(),ax_rx();
  110.  
  111.     if(current == NULLSESSION || current->type == FREE)
  112.         return 0;
  113.     mode = CONV_MODE;
  114.     switch(current->type){
  115.     case TELNET:
  116.         if(current->cb.telnet->remote[TN_ECHO])
  117.             raw();    /* Re-establish raw mode if it was set */
  118.         rcv_char(current->cb.telnet->tcb,0); /* Get any pending input */
  119.         break;
  120.     case FTP:
  121.         ftpccr(current->cb.ftp->control,0);
  122.         break;
  123. #ifdef    AX25
  124.     case AX25TNC:
  125.         ax_rx(current->cb.ax25_cb,0);
  126.         break;
  127. #endif
  128.     }
  129.     return 0;
  130. }
  131. doclose(argc,argv)
  132. int argc;
  133. char *argv[];
  134. {
  135.     struct session *s;
  136.  
  137.     if((s = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
  138.         printf(badsess);
  139.         return -1;
  140.     }
  141.     switch(s->type){
  142.     case TELNET:
  143.         close_tcp(s->cb.telnet->tcb);
  144.         break;
  145.     case FTP:
  146.         close_tcp(s->cb.ftp->control);
  147.         break;
  148. #ifdef    AX25
  149.     case AX25TNC:
  150.         disc_ax25(s->cb.ax25_cb);
  151.         break;
  152. #endif
  153.     }
  154.     return 0;
  155. }
  156. doreset(argc,argv)
  157. int argc;
  158. char *argv[];
  159. {
  160.     long htol();
  161.     struct session *s;
  162.  
  163.     if((s = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
  164.         printf(badsess);
  165.         return -1;
  166.     }
  167.     switch(s->type){
  168.     case TELNET:
  169.         reset_tcp(s->cb.telnet->tcb);
  170.         break;
  171.     case FTP:
  172.         reset_tcp(s->cb.ftp->control);
  173.         break;
  174. #ifdef    AX25
  175.     case AX25TNC:
  176.         reset_ax25(s->cb.ax25_cb);
  177.         break;
  178. #endif
  179.     }
  180.     return 0;
  181. }
  182. int
  183. dokick(argc,argv)
  184. int argc;
  185. char *argv[];
  186. {
  187.     long htol();
  188.     void tcp_timeout();
  189.     struct session *s;
  190.  
  191.     if((s = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
  192.         printf(badsess);
  193.         return -1;
  194.     }
  195.     switch(s->type){
  196.     case TELNET:
  197.         if(kick_tcp(s->cb.telnet->tcb) == -1){
  198.             printf(notval);
  199.             return 1;
  200.         }
  201.         break;
  202.     case FTP:
  203.         if(kick_tcp(s->cb.ftp->control) == -1){
  204.             printf(notval);
  205.             return 1;
  206.         }
  207.         break;
  208. #ifdef    AX25
  209.     case AX25TNC:
  210.         if(kick_ax25(s->cb.ax25_cb) == -1){
  211.             printf(notval);
  212.             return 1;
  213.         }
  214.         return 1;
  215. #endif
  216.     }
  217.     return 0;
  218. }
  219. struct session *
  220. newsession()
  221. {
  222.     register int i;
  223.  
  224.     for(i=0;i<nsessions;i++)
  225.         if(sessions[i].type == FREE)
  226.             return &sessions[i];
  227.     return NULLSESSION;
  228. }
  229. freesession(s)
  230. struct session *s;
  231. {
  232.     if(s == NULLSESSION)
  233.         return;
  234.     if(s->record != NULLFILE){
  235.         fclose(s->record);
  236.         s->record = NULLFILE;
  237.     }
  238.     if(s->rfile != NULLCHAR){
  239.         free(s->rfile);
  240.         s->rfile = NULLCHAR;
  241.     }
  242.     if(s->upload != NULLFILE){
  243.         fclose(s->upload);
  244.         s->upload = NULLFILE;
  245.     }
  246.     if(s->ufile != NULLCHAR){
  247.         free(s->ufile);
  248.         s->ufile = NULLCHAR;
  249.     }
  250.     if(s->name != NULLCHAR){
  251.         free(s->name);
  252.         s->name = NULLCHAR;
  253.     }
  254.     s->type = FREE;
  255. }
  256. /* Control session recording */
  257. dorecord(argc,argv)
  258. int argc;
  259. char *argv[];
  260. {
  261.     if(current == NULLSESSION){
  262.         printf("No current session\n");
  263.         return 1;
  264.     }
  265.     if(argc > 1){
  266.         if(current->rfile != NULLCHAR){
  267.             fclose(current->record);
  268.             free(current->rfile);
  269.             current->record = NULLFILE;
  270.             current->rfile = NULLCHAR;
  271.         }
  272.         /* Open new record file, unless file name is "off", which means
  273.          * disable recording
  274.          */
  275.         if(strcmp(argv[1],"off") != 0
  276.          && (current->record = fopen(argv[1],binmode[APPEND_BINARY])) != NULLFILE){
  277.             current->rfile = malloc((unsigned)strlen(argv[1])+1);
  278.             strcpy(current->rfile,argv[1]);
  279.         }
  280.     }
  281.     if(current->rfile != NULLCHAR)
  282.         printf("Recording into %s\n",current->rfile);
  283.     else
  284.         printf("Recording off\n");
  285.     return 0;
  286. }
  287. /* Control file transmission */
  288. doupload(argc,argv)
  289. int argc;
  290. char *argv[];
  291. {
  292.     struct tcb *tcb;
  293. #ifdef    AX25
  294.     struct ax25_cb *axp;
  295. #endif
  296.  
  297.     if(current == NULLSESSION){
  298.         printf("No current session\n");
  299.         return 1;
  300.     }
  301.     if(argc > 1){
  302.         switch(current->type){
  303.         case TELNET:
  304.             tcb = current->cb.telnet->tcb;
  305.             break;
  306. #ifdef    AX25
  307.         case AX25TNC:
  308.             axp = current->cb.ax25_cb;
  309.             break;
  310. #endif
  311.         case FTP:
  312.             printf("Uploading on FTP control channel not supported\n");
  313.             return 1;
  314.         }
  315.         if(strcmp(argv[1],"stop") == 0 && current->upload != NULLFILE){
  316.             /* Abort upload */
  317.             fclose(current->upload);
  318.             current->upload = NULLFILE;
  319.             if(current->ufile != NULLCHAR){
  320.                 free(current->ufile);
  321.                 current->ufile = NULLCHAR;
  322.             }
  323.         }
  324.         /* Open upload file */
  325.         if((current->upload = fopen(argv[1],binmode[READ_BINARY])) == NULLFILE){
  326.             printf("Can't read %s\n",argv[1]);
  327.             return 1;
  328.         }
  329.         current->ufile = malloc((unsigned)strlen(argv[1])+1);
  330.         strcpy(current->ufile,argv[1]);
  331.         /* All set, kick transmit upcall to get things rolling */
  332.         switch(current->type){
  333. #ifdef    AX25
  334.         case AX25TNC:
  335.             (*axp->t_upcall)(axp,axp->paclen * axp->maxframe);
  336.             break;
  337. #endif
  338.         case TELNET:
  339.             (*tcb->t_upcall)(tcb,tcb->snd.wnd - tcb->sndcnt);
  340.             break;
  341.         }
  342.     }
  343.     if(current->ufile != NULLCHAR)
  344.         printf("Uploading %s\n",current->ufile);
  345.     else
  346.         printf("Uploading off\n");
  347.     return 0;
  348. }
  349.